package cn.wawi.service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import cn.wawi.model.business.Member;
import cn.wawi.model.business.Orders;
import cn.wawi.model.business.OrdersPacket;
import cn.wawi.model.business.OrdersPacketService;
import cn.wawi.model.business.Packet;
import cn.wawi.model.business.Packetitem;
import cn.wawi.utils.SqlHelper;
import cn.wawi.utils.StringUtil;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.tx.Tx;

public class MemberServiceI {

	
	// 直冲
		public void member_chongzhi_zhi(Integer userId, Integer memberId, BigDecimal money, Integer count, Integer time,
				Integer serviceId, String serviceName) throws Exception{

			if (!(userId > 0) || !(memberId > 0)) {
				throw new Exception("员工Id或会员id为空!");
			}
			
			
	       
	        
	        
			//TODo 这里先查用户表,使用 用户表的count或者time和传进来的进行比较,多的增加一条,
			//少的就搞个负的记录
            
			
			
			
			
			Orders order = new Orders();

			order.setMemberId(memberId);
			order.setUserId(userId);
			order.setCreateTime(new Date());
			// 保存orders
			order.save();

			// 订单套餐表
			OrdersPacket op = new OrdersPacket();

			op.setOrderId(order.getId());
			op.setMemberId(memberId);
			op.setUserId(userId);
			op.setPacketId(0);
			op.setPacketName("直充");
			op.setRemark("直充");
			op.setSubCount(1);
			op.setMoney(money);
			op.setCreateTime(new Date());
			op.save();

			
			// 具体记录表
			OrdersPacketService ops = new OrdersPacketService();
			ops.setCount(count);
			ops.setCreateTime(new Date());
			ops.setMemberId(memberId);
			ops.setUserId(userId);
			ops.setMoney(money);
			ops.setOrderId(order.getId());
			ops.setPacketId(op.getId());
			ops.setPacketName(op.getPacketName());
			ops.setServiceId(serviceId);
			ops.setServiceName(serviceName);
			ops.setTime(time);
			ops.save();
			
			order.setMoney(money);
			order.setCount(10000000); //这里要改,先比较
			order.setTime(1000000); //这里也是要改,先比较
			order.update();
		}

		@Before({ Tx.class })
		public void member_chongzhi_packet(String packetIds, Integer userId, Integer memberId) throws Exception {

			if (!(userId > 0) || !(memberId > 0)) {
				throw new Exception("员工Id或会员id为空!");
			}
			Orders order = new Orders();

			order.setMemberId(memberId);
			order.setUserId(userId);
			order.setCreateTime(new Date());

			// 保存orders
			order.save();

			if (StringUtil.notBlank(packetIds)) { // 套餐充值

				
				Object [] ids =  packetIds.split(",");
				
				List<Packet> packets = Packet.dao.find(SqlHelper.sqlParametersWithArray("select * from packet where id in (?) and deleted !=1",ids.length),ids);
						
				
				List<Packetitem> packetItems = Packetitem.dao
						.find(SqlHelper.sqlParametersWithArray("select pt.*,sc.`name` as serviceName from packetitem as pt left join serviceitem as sc on pt.serviceId = sc.id where pt.id in(?) ",ids.length),ids);

				BigDecimal totalMoney = BigDecimal.ZERO;

				Integer pTotalCount = 0;
				Integer pTotalTime = 0;

				for (Packet packet : packets) {

					OrdersPacket op = new OrdersPacket();
					op.setOrderId(order.getId());
					op.setMemberId(memberId);
					op.setUserId(userId);
					op.setPacketId(packet.getId());
					op.setPacketName(packet.getName());
					op.setSubCount(packetItems.size());

					op.setMoney(packet.getMoney());
					op.setCreateTime(new Date());
					op.setRemark(packet.getRemarker());
					op.save();

					Integer ptTotalCount = 0;
					Integer ptTotalTime = 0;
					for (int i = 0; i < packetItems.size(); i++) {

						Packetitem pt = packetItems.get(i);
						
						if (pt.getId().equals(packet.getId())) {
							OrdersPacketService ops = new OrdersPacketService();
							ops.setCount(pt.getCount());
							ops.setCreateTime(new Date());
							ops.setMemberId(memberId);
							ops.setUserId(userId);
							ops.setMoney(pt.getMoney());
							ops.setOrderId(order.getId());
							ops.setPacketId(op.getId());
							ops.setPacketName(op.getPacketName());
							ops.setServiceId(pt.getServiceId());
							ops.setServiceName(pt.getStr("serviceName"));
							ops.setTime(pt.getTime());
							ops.save();
	
							packetItems.remove(i);
							i--;

							ptTotalCount += ops.getCount();
							ptTotalTime += ops.getTime();
						}
					}
					op.setTime(ptTotalTime);
					op.setCount(ptTotalCount);
					op.update();
					pTotalCount += ptTotalCount;
					pTotalTime += ptTotalTime;
					totalMoney = totalMoney.add(packet.getMoney());
				}

				order.setMoney(totalMoney);
				order.setCount(pTotalCount);
				order.setTime(pTotalTime);
				order.update();
			}
		}
	
}
